﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.WebApp.ViewModel;
using System.Web.Mvc;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Infrastructure;
using XSTDZ.ERP.Core.Extentions;

namespace XSTDZ.ERP.WebApp.Models
{

    public enum RefeTypeOption
    { 
        财务账单,
        性别,
        票据方式,
        支付方式,
        企业性质,
        计量单位,
        证件类型,
        工作组名称,
        项目名称
    }
    /// <summary>
    /// 组织机构类
    /// </summary>
    public class RefeModel
    {
        private readonly IRefeService refeServiceImpl = ServiceLocator.Instance.GetService<IRefeService>();//服务加载器
        /// <summary>
        /// 根据模块名称获取该类别下的所有列表
        /// </summary>
        /// <param name="moduleName">模块名</param>
        /// <returns></returns>
        public IList<RefeDto> GetRefeByModuleName(string moduleName)
        {
            return refeServiceImpl.GetRefeDtosByModuleName(moduleName).ToList();
        }
        public IList<RefeDto> GetRefeByModuleName(RefeTypeOption refeTypeOption)
        {
            var moduleName = refeTypeOption.ToString();
            return refeServiceImpl.GetRefeDtosByModuleName(moduleName).ToList();
        }
        /*
        public IList<RefeViewModel> ConvertToUiData(IEnumerable<RefeTypeDto> refeTypeDto)
        {
            IList<RefeViewModel> lst = new List<RefeViewModel>();
            if (refeTypeDto == null || refeTypeDto.Count() == 0)
            {
                return null;
            }
            foreach (var item in refeTypeDto)
            {
                if (item.RefeDtos == null || item.RefeDtos.Count == 0)
                {
                    lst.Add(new RefeViewModel { 
                         TypeId=item.Id,
                          TypeName=item.ModuleName,
                           TypeModuleName=item.TypeName
                    });
                }
                else
                { 
                    foreach(var c in item.RefeDtos)
                    {
                        lst.Add(new RefeViewModel { 
                         TypeId=item.Id,
                          TypeName=item.ModuleName,
                           TypeModuleName=item.TypeName,

                            ID=c.Id,
                             Code=c.RefeCode,
                              Name=c.RefeName,
                               State=c.Deleted!=0?"禁用":""
                    });
                    }
                }
            }
            return lst;
        }
        */
        /// <summary>
        /// 
        /// </summary>
        /// <param name="refeTypeId">类别ID（大类别ID [保存在初始化数据中，不可更改]）</param>
        /// <param name="selectRefeTypeId">选中的小类别ID(可空）</param>
        /// <returns></returns>
        public IList<SelectListItem> GetDropDownListItemsByCode(IList<RefeDto> items,string selectRefeCode="")
        {
           var selectListItems= items.Select(r => new SelectListItem
               {
                   Text = r.RefeName,
                   Value = r.RefeCode.ToString(),
                   Selected = !string.IsNullOrEmpty(selectRefeCode) && selectRefeCode == r.RefeCode.ToString()
               }).ToList();
           return selectListItems ?? new List<SelectListItem>(0);
        }
        public IList<SelectListItem> GetDropDownListItemsById(IList<RefeDto> items, string selectRefeId = "")
        {
            var selectListItems = items.Select(r => new SelectListItem
            {
                Text = r.RefeName,
                Value = r.Id.ToString(),
                Selected = !string.IsNullOrEmpty(selectRefeId) && selectRefeId == r.RefeCode.ToString()
            }).ToList();
            return selectListItems ?? new List<SelectListItem>(0);
        }
        /// <summary>
        /// 按GUID来判断
        /// </summary>
        /// <param name="refeTypeId">类别ID（大类别ID [保存在初始化数据中，不可更改]）</param>
        /// <param name="selectRefeTypeId">选中的小类别ID(可空）</param>
        /// <returns></returns>
        public IList<SelectListItem> GetDropDownListItems(string refeTypeId,Guid? selectRefeTypeId=null)
        { 
              var items = refeServiceImpl.GetRefeDtos(Guid.Parse(refeTypeId))
                 .Select(r => new SelectListItem { Text = r.RefeName, Value = r.Id.ToString(), 
                     Selected = selectRefeTypeId != null && selectRefeTypeId.Value.ToString()==r.Id.ToString() }).ToList();

              //if (!string.IsNullOrEmpty(selectTypeId))
              //{
              //    items.Where(i => i.Value == selectTypeId).First().Selected = true;
              //}

              return items ?? new List<SelectListItem>(0);
        }
        ///// <summary>
        ///// 按指定的编码来判断(如性别：男,女简单类别，数据库表中不是存的GUID，而是一个smallint,故存refeCode )
        ///// </summary>
        ///// <param name="refeTypeId">类别ID（大类别ID [保存在初始化数据中，不可更改]）</param>
        ///// <param name="customValue">选中的编号</param>
        ///// <returns></returns>
        //public IList<SelectListItem> GetDropDownListItems(string refeTypeId, string? customValue = null)
        //{
        //    IRefeService refeServiceImpl = ServiceLocator.Instance.GetService<IRefeService>();//服务加载器

        //    var items = refeServiceImpl.GetRefeDtos(Guid.Parse(refeTypeId))
        //        .Select(r => new SelectListItem
        //        {
        //            Text = r.RefeName,
        //            Value = r.RefeCode.ToString(),
        //            Selected = customValue != null && customValue.Value.ToString() == r.RefeCode.ToString()
        //        }).ToList();
        //    return items ?? new List<SelectListItem>(0);
        //}
    }
       
}